package cn.xie.blog.serviceuser.controller;

import cn.xie.blog.commonutil.response.Response;
import cn.xie.blog.model.common.enums.response.ResponseCodeEnum;
import cn.xie.blog.model.common.param.login.LoginParam;
import cn.xie.blog.model.common.param.registered.RegisteredParam;
import cn.xie.blog.serverutil.constant.common.RequestConstant;
import cn.xie.blog.serverutil.exception.BlogException;
import cn.xie.blog.serviceuser.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ServerWebExchange;

import java.net.http.HttpRequest;


@RestController
@RequestMapping(value="/user")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserController {
    Logger log = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;
    /**
     * @description: 用户登录
     * @author: xiaoxie
     * @date: 2025/4/22 下午8:37
     * @param: 
     * @return: 
     **/
    @PostMapping("/login")
    public Response<String> userLogin(@RequestBody LoginParam loginParam) {
        if(loginParam.getUsername() == null || loginParam.getPassword() == null){
            log.warn(ResponseCodeEnum.PARAM_ERROR.getMessage());
            throw new BlogException(ResponseCodeEnum.PARAM_ERROR);
        }
        return Response.ok(userService.login(loginParam));
    }
    /**
     * @description: 用户注册
     * @author: xiaoxie
     * @date: 2025/4/22 下午9:45
     * @param:
     * @return:
     **/
    @PostMapping("/registered")
    public Response<Boolean> registered(@RequestBody RegisteredParam registeredParam) {
        if(registeredParam.getUsername() == null || registeredParam.getPassword() == null){
            log.warn(ResponseCodeEnum.PARAM_ERROR.getMessage());
            throw new BlogException(ResponseCodeEnum.PARAM_ERROR);
        }
        return Response.ok(userService.registered(registeredParam));
    }
    @GetMapping("/code/{mail}")
    public Response<Boolean> getCode(@PathVariable("mail") String mail) {
        return Response.ok(userService.getCode(mail));
    }
    @GetMapping("/logout")
    public Response<Boolean> logout() {
        return Response.ok(userService.logout());
    }
}
